package com.wangp.springbootsource.pro;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

/**
 * <pre>
 * classname MyAwareProcessor
 * description
 * </pre>
 *
 * @author wangpeng
 * @date 2020/9/27 19:48
 **/
@Component
public class MyAwareProcessor implements BeanPostProcessor {

    private final ConfigurableApplicationContext configurableApplicationContext;

    public MyAwareProcessor(ConfigurableApplicationContext configurableApplicationContext) {
        this.configurableApplicationContext = configurableApplicationContext;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof MyAware) {
            ((MyAware) bean).setFlag(configurableApplicationContext.getBean(Flag.class));
        }
        return bean;
    }
}
